package com.ruoyi.project.ins.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.project.ins.domain.InsRegister;
import com.ruoyi.project.ins.service.IInsRegisterService;
import com.ruoyi.project.mapper.InsRegisterMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class InsRegisterServiceImpl implements IInsRegisterService {

    @Autowired
    private InsRegisterMapper insRegisterMapper;

    @Autowired
    protected Validator validator;

    @Override
    public List<InsRegister> selectRegisterList(InsRegister insRegister) {
        return insRegisterMapper.selectRegisterList(insRegister);
    }

    @Override
    public String importRegister(List<InsRegister> insRegisterList, String operateName) {
        if (CollectionUtils.isEmpty(insRegisterList)) {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (InsRegister insRegister : insRegisterList)
        {
            try
            {
                // 验证邮箱是否已存在
                InsRegister ins = insRegisterMapper.selectByEmail(insRegister.getEmailAccount());
                if (ObjectUtils.isEmpty(ins)) {
                    BeanValidators.validateWithException(validator, insRegister);
                    insRegister.setRandomUserAccount();
                    insRegister.setDefaultStatus();
                    insRegister.setCreateBy(operateName);
                    insRegister.generateCodeAddress();
                    insRegisterMapper.insertInsRegister(insRegister);
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、注册邮箱 ")
                            .append(insRegister.getEmailAccount()).append(" 导入成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("、注册邮箱 ")
                            .append(insRegister.getEmailAccount()).append(" 已被使用");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、注册邮箱 " + insRegister.getEmailAccount() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int insertInsRegister(InsRegister insRegister) {
        return insRegisterMapper.insertInsRegister(insRegister);
    }

    @Override
    public int updateInsRegister(InsRegister insRegister) {
        return insRegisterMapper.updateInsRegister(insRegister);
    }

    @Override
    public InsRegister selectRegisterById(Long id) {
        return insRegisterMapper.selectRegisterById(id);
    }

    @Override
    public int deleteByIds(Long[] deleteIds) {
        return insRegisterMapper.deleteByIds(deleteIds);
    }

    @Override
    public InsRegister selectByEmail(String emailAccount) {
        return insRegisterMapper.selectByEmail(emailAccount);
    }

    @Override
    public List<InsRegister> selectByIds(Long[] ids) {
        return insRegisterMapper.selectByIds(ids);
    }
}
